package main

import (
	"context"
	"errors"
	"fmt"

	"gitee.com/wu-jin-feng/kinfu/fgin"
	reflectPb "gitee.com/wu-jin-feng/kinfu/fgin/demos/fplugs_grpc_test/pb"
	_ "gitee.com/wu-jin-feng/kinfu/fgin/demos/fplugs_grpc_test/plugs"
)

type DemoServiceServer struct {
	reflectPb.UnimplementedDemoServiceServer
}

func (s *DemoServiceServer) GetScheme(ctx context.Context, req *reflectPb.GetSchemeReq) (*reflectPb.GetSchemeResp, error) {
	if req.Info == nil {
		return nil, errors.New("info 不能为空")
	}
	resp, err := grpcClient.GetScheme(ctx, &reflectPb.GetSchemeReq{
		Path: "123",
		Info: &reflectPb.GetSchemeRespInfo{
			Info: "xxxxxx",
			Name: "gogopythonpython",
		},
	})
	if err != nil {
		return nil, err
	}
	fmt.Println("grpc test_web1 resp:", resp)
	endData := &reflectPb.GetSchemeResp{
		Data: req.Path + ",gogogo",
		Info: &reflectPb.GetSchemeRespInfo{
			Info: fmt.Sprintf("这是一个info,%s", req.Info.Info),
			Name: fmt.Sprintf("作者是%s", req.Info.Name),
		},
	}
	return endData, nil
}

var (
	grpcClient reflectPb.DemoServiceClient
)

func main() {
	f := fgin.Fgin{}
	s := f.GetGrpcServer("./test.yaml")
	reflectPb.RegisterDemoServiceServer(s, &DemoServiceServer{})
	conn, _ := f.GetFgrpcClientConn("test_web1")
	grpcClient = reflectPb.NewDemoServiceClient(conn)
	f.RunGrpc(s)
}
